<?php

defined('SYSPATH') or die('No direct script access.');

class Area
{

    private static $instances;
    private static $areas;
    private $data;

    public static function & instances($id = 0)
    {

        if (!isset(self::$instances[$id]))
        {
            $class = __CLASS__;
            self::$instances[$id] = new $class($id);
        }
        return self::$instances[$id];
    }

    public function __construct($id)
    {

        $this->data = NULL;
        $this->get_areas_rec($id);
        $this->_load($id);
    }

    public function _load($id)
    {

        if (!$id)
            return FALSE;

        $result = ORM::factory('area')
                ->where('id_area', '=', $id)
                ->find()
                ->as_array();

        $this->data = $result ? $result : array();
    }

    public function get($key = NULL)
    {

        if (is_null($key))
        {
            return $this->data;
        }
        else
        {
            return isset($this->data[$key]) ? $this->data[$key] : NULL;
        }
    }

    public function set($data)
    {

        $area = ORM::factory('area');
        $area->values($data);
        if ($area->check())
        {
            $area->save();
            if ($area->saved())
            {
                return $area->id_area;
            }
        }

        return false;
    }

    public function update($data)
    {
        $area = ORM::factory('area')
                ->where('id_area', '=', $this->data['id_area'])
                ->find();

        if (!$area->loaded())
        {
            return false;
        }
        else
        {
            $area->values($data);
            if ($area->check())
            {
                $area->save();
                if ($area->saved())
                {
                    return true;
                }
            }
        }
    }

    public function delete()
    {
        $area = ORM::factory('area', $this->data['id_area']);
        if ($area->loaded())
        {
            $this->delete_image($this->data['id_area']);

            return $area->delete();
        }
        else
        {
            return FALSE;
        }
    }

    public function get_areas_rec($id_parent = 0, $pre = "")
    {

        $area_category = DB::select()
                ->from('areas')
                ->where('id_parent', '=', $id_parent)
                ->and_where('active', '=', 1)
                ->order_by('position')
                ->execute()
                ->as_array();


        foreach ($area_category as $key => $value)
        {
            $value['pre'] = $pre;
            self::$areas[$value['id_area']] = $value;
            if ($value['endnode'] > 0)
            {
                $this->get_areas_rec($value['id_area'], $pre . '--');
            }
        }
    }

    public function get_areas()
    {
        return self::$areas;
    }

    public function get_areas_norec($id_parent = 0)
    {

        $area = array();
        $area_category = DB::select()
                ->from('areas')
                ->where('id_parent', '=', $id_parent)
                ->and_where('active', '=', 1)
                ->order_by('position')
                ->execute()
                ->as_array();

        $count = 0;
        while ($count < count($area_category))
        {
            $area[$area_category[$count]['id_area']] = $area_category[$count];
            $count++;
        }


        return $area;
    }

    public function get_a_area($id_area)
    {
        $area = array();
        $area = DB::select()
                ->from('areas')
                ->where('id_area', '=', $id_area)
                ->and_where('active', '=', 1)
                ->execute()
                ->as_array();
        return $area[0];
    }

    public function add_image($id_area, $url_image)
    {
        $image = ORM::factory('area_image');
        $image->id_area = $id_area;
        $image->url_image = $url_image;

        if ($image->check())
        {
            $image->save();
            if ($image->saved())
            {
                return true;
            }
        }
    }

    public function delete_image($id_area)
    {

        $image = ORM::factory('area_image')
                ->where('id_area', '=', $id_area)
                ->find();

        if ($image->loaded())
        {
            $image->delete();
        }
    }

    public function update_image($id_area, $url_image)
    {
        $image = ORM::factory('area_image')
                ->where('id_area', '=', $id_area)
                ->find();

        if ($image->loaded())
        {
            $image->url_image = $url_image;
            if ($image->check())
            {
                $image->save();
            }
        }
    }

}

